//
//  BKOrderPayForResultsViewController.m
//  bestkeep
//
//  Created by 马乾征 on 16/11/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKOrderPayForResultsViewController.h"
#import "BKOrderPaymentViewController.h"
#import "Mediator+BKMain.h"
#import "Mediator+BKMyOrderDetail.h"
#import "Mediator+BKMy.h"
#import "BK_OD_Color.h"
NSString *const ODCertifyDetail = @"/getCertifyDetail"; //实名认证信息

@interface BKOrderPayForResultsViewController ()<UIAlertViewDelegate>

@property(nonatomic, strong)UIImageView *BGView;
@property(nonatomic, strong)UIImageView *imgV_icon;
@property(nonatomic, strong)UILabel     *lab_result;
@property(nonatomic, strong)UIButton    *btn_left;
@property(nonatomic, strong)UIButton    *btn_right;

@end

@implementation BKOrderPayForResultsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setProperty];
    [self createUI];
    [self setViewContent];
    [self aboutReal_nameAuthenticationRelated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.title = kOrderLocalizeString(@"BKOrderPayForResults");
    self.view.backgroundColor = ThemeBackgroundColor;
    
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _BGView = [[UIImageView alloc] init];
    _BGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_BGView];
    
    _imgV_icon = [[UIImageView alloc] init];
    [_BGView addSubview:_imgV_icon];
    
    _lab_result = [[UILabel alloc] init];
    _lab_result.backgroundColor = [UIColor clearColor];
    _lab_result.font = UIFontCustom(20);
    [_BGView addSubview:_lab_result];
    
    _btn_left = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn_left.backgroundColor = [UIColor whiteColor];
    [_btn_left setTitleColor:kOrderBlack1 forState:UIControlStateNormal];
    _btn_left.titleLabel.font = UIFontCustom(15);
    _btn_left.layer.cornerRadius = 5;
    [_btn_left addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn_left];
    
    _btn_right = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn_right.backgroundColor = kOrderBKGreen;
    [_btn_right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _btn_right.titleLabel.font = UIFontCustom(15);
    _btn_right.layer.cornerRadius = 5;
    [_btn_right addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn_right];
    
    [self makeConstraints];
    
}

- (void)makeConstraints{
    
    [_BGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(74);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(90);
    }];
    
    CGSize lable_size = [@"四字大小" sizeWithAttributes:@{NSFontAttributeName : UIFontCustom(20)}];
    CGFloat spacing = 10;
    CGFloat imageWidth = lable_size.height - 3;
    [_imgV_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_BGView.mas_centerX).offset(-(lable_size.width + imageWidth + spacing) / 2.0);
        make.centerY.mas_equalTo(_BGView);
        make.width.height.mas_equalTo(imageWidth);
    }];
    
    [_lab_result mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_imgV_icon.mas_right).offset(spacing);
        make.centerY.mas_equalTo(_BGView);
    }];
    
    [_btn_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(_BGView.mas_bottom).offset(20);
        make.right.mas_equalTo(self.view.mas_centerX).offset(-22);
        make.height.mas_equalTo(40);
    }];
    
    [_btn_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(_BGView.mas_bottom).offset(20);
        make.left.mas_equalTo(self.view.mas_centerX).offset(22);
        make.height.mas_equalTo(40);
    }];
    
}

- (void)setViewContent{
    UIImage     *img_resultIcon;
    NSString    *resultText;
    UIColor     *textColor;
    NSString    *leftTitle;
    NSString    *righTitle;
    if (_success) {
        img_resultIcon = [UIImage imageNamed:@"BKOrder_selected"];
        textColor  = kOrderBKGreen;
        resultText = kOrderLocalizeString(@"BKOrderPaymentSuccess");
        leftTitle  = @"查看订单详情";
        righTitle  = @"继续购物";
    }else{
        img_resultIcon = [UIImage imageNamed:@"BKOrder_failure"];
        textColor  = kOrderPriceRed;
        resultText = kOrderLocalizeString(@"BKOrderPaymentFailure");
        leftTitle  = @"查看订单详情";
        righTitle  = @"再试一次";
    }
    
    _imgV_icon.image = img_resultIcon;
    _lab_result.text = resultText;
    _lab_result.textColor = textColor;
    [_btn_left setTitle:leftTitle forState:UIControlStateNormal];
    [_btn_right setTitle:righTitle forState:UIControlStateNormal];
    
    if (_enterForeground) {
        _btn_left.hidden = YES;
        _btn_right.hidden = YES;
        self.navigationItem.hidesBackButton = NO;
    }
}


/**
    left btn action
 */
- (void)leftBtnAction:(UIButton *)sender{
    UIViewController *orderDetailVC = [[Mediator sharedInstance] GetBKMyOrderDetailController:_orderNumber ? _orderNumber : @""];
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

/**
    right btn action
 */
- (void)rightBtnAction:(UIButton *)sender{
    if ([sender.titleLabel.text isEqualToString:@"继续购物"]) {

        [self.navigationController popToRootViewControllerAnimated:YES];
    }else if ([sender.titleLabel.text isEqualToString:@"再试一次"]){

        [self.navigationController popViewControllerAnimated:YES];
    
    }
}

//实名认证相关   成功 && 跨境淘 && 未实名认证 -> 去认证
- (void)aboutReal_nameAuthenticationRelated{
    NSString *UserAuthStatus = [[Mediator sharedInstance] BKMyUserInfo][@"BKMyUserInfoUserAuthStatusKey"];
    if (_success && [_globalStatus isEqualToString:@"1"] && ![UserAuthStatus isEqualToString:@"已认证"]){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"根据海关规定，购买跨境淘商品需要办理清关手续，错误信息可能导致无法正常清关，请您配合进行实名认证" delegate:self cancelButtonTitle:@"去认证" otherButtonTitles:@"知道了", nil];
        [alertView show];
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
        __weak typeof(self)weakSelf = self;
        [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODCertifyDetail) parameters:@{} success:^(id  _Nullable responseObject) {
            BOOL success = [responseObject[@"success"] boolValue];
            if (!success) {
                return;
            }
            [[Mediator sharedInstance] realNameAuthVCWithNav:weakSelf.navigationController dataParaDict:responseObject];
        } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            
        }];
    }else{

    }
}
@end

















































